
import time

from dexen.user.libs import constants as dc, server_api

from libs import input_reading, constants 

""" GLOBALS """
pop_api = server_api.PopAPIFactory() 
boxes = input_reading.read_input()
n_boxes = len(boxes)

def is_left(item1, item2):
    if item1['x'] < item2['x'] and item1['y'] < item2['y']:
        return True;
    else:
        return False; 

def is_below(item1, item2):
    if item1['x'] < item2['x'] and item1['y'] > item2['y']:
        return True;
    else:
        return False; 

def calculate(individual):

    genotype = individual[constants.GENOTYPE]
    phenotype = [None] * n_boxes

    """ The indices are not referring to the indices of boxes """
    gamma_x = genotype['gamma_x']
    gamma_y = genotype['gamma_y']

    """ The indices refers to the actual indices in boxes """
    encoding = genotype['encoding']
    orient = genotype['orient']

    for i in range(n_boxes):
        phenotype[i] = {}

    """
    Calculates the x coordinates of boxes  
    """
    phenotype[gamma_x[0]]["x"] = 0
    for i in range(1, n_boxes):
        box1_no = gamma_x[i]
        max_found = 0
        for j in range(0, i):
            box2_no = gamma_x[j]
            side = 0
            if is_left(encoding[box2_no], encoding[box1_no]):
                if orient[box2_no] == 0:
                    side = boxes[box2_no]['w']
                else:
                    side = boxes[box2_no]['h']
                if phenotype[box2_no]['x'] + side > max_found:
                    max_found = phenotype[box2_no]['x'] + side
        phenotype[box1_no]["x"] = max_found
        #phenotype[gamma_x[i]]['x'] = max_found            
    
    """
    Calculates the y coordinates of boxes
    """    
    phenotype[gamma_y[n_boxes - 1]]['y'] = 0
    for i in range(n_boxes-2, -1, -1):
        max_found = 0
        box1_no = gamma_y[i]
        for j in range(i + 1, n_boxes):
            box2_no = gamma_y[j]
            side = 0
            if is_below(encoding[box2_no], encoding[box1_no]):
                if (orient[box2_no] == 0):
                    side = boxes[box2_no]['h']
                else:
                    side = boxes[box2_no]['w']
                if phenotype[box2_no]['y'] + side > max_found:
                    max_found = phenotype[box2_no]['y'] + side
        phenotype[box1_no]['y'] = max_found            
    
    individual['phenotype'] = phenotype

                             
def develop(individuals):   
    print "I am in development" 
    
    """
    individuals = pop_api.downloadLiveIndividuals(
        select = (constants.GENOTYPE, ), 
        where = (
            (constants.GENOTYPE, dc.NOT_EQUAL, None),
            (constants.PHENOTYPE, dc.EQUAL, None)
        ), 
        count = pop_api.getGlobalData()[constants.DEVELOPMENT]
    )
    """
    #print "******", individuals[0], "******"
    
    for individual in individuals:
        calculate(individual)
    
    #pop_api.uploadIndividuals(individuals)
    return individuals
